#include "head.h" 

//注册用户
int do_register(int sockfd,sqlite3 *pdb,char *_username,char *_password)
{
	char *errmsg;
	char buf[1024];
	char **dbresult;
	int nrow = 0,ncolumn = 0;
	char sql[1024] = {0};
	mhead_t *head = (mhead_t *)buf;	
	
	//查询用户名是否存在
	sprintf(sql,"select * from user_table where NAME='%s';",_username);
	if(sqlite3_get_table(pdb,sql,&dbresult,&nrow,&ncolumn,&errmsg) != 0)
	{
		fprintf(stderr,"sqlite3 get table error : %s.\n",errmsg);
		exit(EXIT_FAILURE);
	}

	//没有这样的用户名  注册
	if(nrow == 0)
	{
		//录入数据库
		bzero(sql,sizeof(sql));
		sprintf(sql,"insert into user_table values('%s','%s');",_username,_password);
		EXEC_SQL(pdb,sql,errmsg);//封装 sqlite3_exec()
		
		printf("ok ........\n");
		
		//封装一个应答包给客户端
		head->type = USER_SUCCESS;
		if(send(sockfd,buf,sizeof(mhead_t),0) < 0)
		{
			perror("Fail to send");
			exit(EXIT_FAILURE);
		}
	//注册失败，用户名存在
	}else{
		head->type = USER_FAILURE;
		if(send(sockfd,buf,sizeof(mhead_t),0) < 0)
		{
			perror("Fail to send");
			exit(EXIT_FAILURE);
		}
		//表示未知	
		printf("???????\n");
	}
	//插入到数据库之后，释放dbresult结果
	sqlite3_free_table(dbresult);
	return 0;
}

int do_login(int sockfd,sqlite3 *pdb,char *_username,char *_password)
{
	char *errmsg;
	char buf[1024];
	char **dbresult;
	int nrow = 0,ncolumn = 0;
	char sql[1024] = {0};
	mhead_t *head = (mhead_t *)buf;	
	
	//查询用户名和密码是否匹配
	bzero(sql,sizeof(sql));
	sprintf(sql,"select * from user_table where NAME='%s' and PASSWORD='%s';",_username,_password);
	if(sqlite3_get_table(pdb,sql,&dbresult,&nrow,&ncolumn,&errmsg) != 0)
	{
		fprintf(stderr,"sqlite3 get table error : %s.\n",errmsg);
		exit(EXIT_FAILURE);
	}

	//查询到该用户
	if(nrow != 0)
	{	
		printf("ok ........\n");
		
		//封装一个应答包给客户端
		head->type = USER_SUCCESS;
		if(send(sockfd,buf,sizeof(mhead_t),0) < 0)
		{
			perror("Fail to send");
			exit(EXIT_FAILURE);
		}
	//用户名或密码错误
	}else{
		head->type = USER_FAILURE;
		if(send(sockfd,buf,sizeof(mhead_t),0) < 0)
		{
			perror("Fail to send");
			exit(EXIT_FAILURE);
		}
		//表示未知	
		printf("???????\n");
	}
	return 0;
}

int do_word(int sockfd,sqlite3 *pdb,char *_word)
{
	char *errmsg;
	char buf[1024];
	char **dbresult;
	int nrow = 0,ncolumn = 0;
	char sql[1024] = {0};
	char explain[1024];
	mhead_t *head = (mhead_t *)buf;	
	
	//查询该单词是否存在
	bzero(sql,sizeof(sql));
	sprintf(sql,"select * from dict_table where word='%s';",_word);
	if(sqlite3_get_table(pdb,sql,&dbresult,&nrow,&ncolumn,&errmsg) != 0)
	{
		fprintf(stderr,"sqlite3 get table error : %s.\n",errmsg);
		exit(EXIT_FAILURE);
	}

		
	strcpy(head->word,dbresult[3]);

	//有该单词
	if(nrow != 0)
	{	
		printf("ok ........\n");
		//封装一个应答包给客户端
		head->type = USER_SUCCESS;
	    if(send(sockfd,buf,sizeof(mhead_t),0) < 0)
		{
			perror("Fail to send");
			exit(EXIT_FAILURE);
		}
		
		//该单词不存在
	}else{
		head->type = USER_FAILURE;
		if(send(sockfd,buf,sizeof(mhead_t),0) < 0)
		{
			perror("Fail to send");
			exit(EXIT_FAILURE);
		}
		//表示未知	
		printf("???????\n");
	}
	return 0;
}


int do_client(int sockfd,sqlite3 *pdb)
{
	int n;
	int count = 0;
	char buf[1024];
	//强制转换成结构体指针，方便访问协议内容
	mhead_t *head = (mhead_t *)buf;	//

	


		//处理客户端业务
		count = 0;//接收总的字节数
		//接收协议头,保证接收的数据完整型,防止底层数据粘包
		while(1)
		{
			n = recv(sockfd,buf + count,sizeof(mhead_t) - count,0);
			if(n < 0)
			{
				perror("Fail to send\n");
				exit(EXIT_FAILURE);
			}
			if(n == 0){
				printf("the client is offline");
				return 0;
			}
			count += n;
			printf("count : %d mhead_t : %ld\n",count,sizeof(mhead_t));
			if(count == sizeof(mhead_t))
				break;
		}
		//解析数据包
		switch(head->type)
		{
		case USER_REGISTER:
			do_register(sockfd,pdb,head->username,head->password);	
			break;
		case USER_LOGIN:
			do_login(sockfd,pdb,head->username,head->password);
			break;
		case USER_WORD:
			do_word(sockfd,pdb,head->word);
			break;
		defalut:
			return 0;
		}	

	return 1;
}
